#include "startup_thread.h"
#include "LED_thread.h"
#include "malloc.h"
#include "lora_thread.h"
#include "usart_thread.h"
#include "config.h"
#include "Switch.h"
#include "heartbeat.h"
static rt_thread_t heartbeat_thread = RT_NULL;

static rt_thread_t lora_thread = RT_NULL;
static rt_thread_t usart1_Send_thread = RT_NULL;
static rt_thread_t usart1_receive_thread = RT_NULL;
static rt_thread_t SW_thread = RT_NULL;

#define MEM1_BLOCK_SIZE 32                             //内存块大小为32字节
#define MEM1_MAX_SIZE 1 * 512                         //内存池大小
__align(32) uint8_t mem1base[MEM1_MAX_SIZE];           //内存池
uint16_t mem1mapbase[MEM1_MAX_SIZE / MEM1_BLOCK_SIZE]; //内存管理表
Mallco_dev mallco_dev;                                 //内存管理器
void startup_thread(void)
{
	my_mem_init(&mallco_dev, mem1base, mem1mapbase, MEM1_MAX_SIZE, MEM1_BLOCK_SIZE);
	Init_Model();
	
	heartbeat_thread =  rt_thread_create("heartbeat",						//线程名字
									Heartbeat_thread_entry,				//线程入口函数
									RT_NULL,						//线程入口函数参数
									Heartbeat_thread_stack_size,		//线程栈大小
									Heartbeat_thread_priority,			//线程优先级
									Heartbeat_thread_tick);				//线程时间片
	if(heartbeat_thread != RT_NULL) rt_thread_startup(heartbeat_thread);
	
	lora_thread =  rt_thread_create("lora",						//线程名字
									lora_thread_entry,				//线程入口函数
									RT_NULL,						//线程入口函数参数
									LoRa_thread_stack_size,		//线程栈大小
									LoRa_thread_priority,			//线程优先级
									LoRa_thread_tick);				//线程时间片
	if(lora_thread != RT_NULL) rt_thread_startup(lora_thread);
	
	usart1_Send_thread = rt_thread_create("usart2_send",						//线程名字
									Usart1_Send_thread_entry,		//线程入口函数
									RT_NULL,						//线程入口函数参数
									usart1_Send_thread_stack_size,	//线程栈大小
									usart1_Send_thread_priority,		//线程优先级
									usart1_Send_thread_tick);		//线程时间片			
	if(usart1_Send_thread != RT_NULL) 
	{
		rt_thread_startup(usart1_Send_thread);
	}
	
	usart1_receive_thread = rt_thread_create("usart2_receive",						//线程名字
									Usart1_Receive_thread_entry,		//线程入口函数
									RT_NULL,						//线程入口函数参数
									usart1_Receive_thread_stack_size,	//线程栈大小
									usart1_Receive_thread_priority,		//线程优先级
									usart1_Receive_thread_tick);		//线程时间片			
	if(usart1_receive_thread != RT_NULL) 
	{
		rt_thread_startup(usart1_receive_thread);
	}
	
	SW_thread = rt_thread_create("SW",						//线程名字
									Switch_thread_entry,		//线程入口函数
									RT_NULL,						//线程入口函数参数
									SW_thread_stack_size,	//线程栈大小
									SW_thread_priority,		//线程优先级
									SW_thread_tick);		//线程时间片			
	if(SW_thread != RT_NULL) 
	{
		rt_thread_startup(SW_thread);
	}
}
